package com.itita.ww2.core;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itita.ww2.enums.WW2ExceptionCode;
import com.google.common.base.Joiner;
import com.itita.ww2.enums.PushCode;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;

public class MessageHandler {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(MessageHandler.class);
	// 
	private static MessageHandler instance = new MessageHandler();

	public static MessageHandler getInstance() {
		return instance;
	}

	private SFSExtension ext;

	public void init(SFSExtension ext) {
		this.ext = ext;
	}

	public void push(String cmd, PushCode code, ISFSObject pushObj, List<User> users) {
		pushObj = setPushObj(code, pushObj);
		if (users != null) {
			ext.send(cmd, pushObj, users);
		}
	}

	public void push(String cmd, PushCode code, ISFSObject pushObj, User user) {
		pushObj = setPushObj(code, pushObj);
		if (user != null) {
			ext.send(cmd, pushObj, user);
		}
	}

	private ISFSObject setPushObj(PushCode code, ISFSObject params) {
		if (params == null) {
			params = new SFSObject();
		}
		if (code != null) {
			params.putInt("__REQID__", code.getValue());
		}
		return params;
	}

	/*
	 * User
	 */
	public void pushUser(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.user", code, pushObj, users);
	}

	public void pushUser(PushCode code, ISFSObject pushObj, User user) {
		push("push.user", code, pushObj, user);
	}

	/*
	 * User Event
	 */
	public void pushUserEvent(String subCMD, PushCode code, ISFSObject pushObj, List<User> users) {
		push(Joiner.on('.').skipNulls().join("push.user.event", subCMD), code, pushObj, users);
	}

	public void pushUserEvent(String subCMD, PushCode code, ISFSObject pushObj, User user) {
		push(Joiner.on('.').skipNulls().join("push.user.event", subCMD), code, pushObj, user);
	}

	/*
	 * User Notice
	 */
	public void pushUserNotice(String subCMD, PushCode code, ISFSObject pushObj, List<User> users) {
		push(Joiner.on('.').skipNulls().join("push.user.notice", subCMD), code, pushObj, users);
	}

	public void pushUserNotice(String subCMD, PushCode code, ISFSObject pushObj, User user) {
		push(Joiner.on('.').skipNulls().join("push.user.notice", subCMD), code, pushObj, user);
	}

	/*
	 * WorldMap
	 */
	public void pushWM(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.wm", code, pushObj, users);
	}

	public void pushWM(PushCode code, ISFSObject pushObj, User user) {
		push("push.wm", code, pushObj, user);
	}

	/*
	 * WorldMap Event
	 */
	public void pushWMEvent(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.wm.event", code, pushObj, users);
	}

	public void pushWMEvent(PushCode code, ISFSObject pushObj, User user) {
		push("push.wm.event", code, pushObj, user);
	}

	/*
	 * WorldMap Notice
	 */
	public void pushWMNotice(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.wm.notice", code, pushObj, users);
	}

	public void pushWMNotice(PushCode code, ISFSObject pushObj, User user) {
		push("push.wm.notice", code, pushObj, user);
	}

	/*
	 * Alliance
	 */
	public void pushAl(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.al", code, pushObj, users);
	}

	public void pushAl(PushCode code, ISFSObject pushObj, User user) {
		push("push.al", code, pushObj, user);
	}

	/*
	 * Alliance Event
	 */
	public void pushAlEvent(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.al.event", code, pushObj, users);
	}

	public void pushAlEvent(PushCode code, ISFSObject pushObj, User user) {
		push("push.al.event", code, pushObj, user);
	}

	/*
	 * Alliance Notice
	 */
	public void pushAlNotice(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.al.notice", code, pushObj, users);
	}

	public void pushAlNotice(PushCode code, ISFSObject pushObj, User user) {
		push("push.al.notice", code, pushObj, user);
	}

	/*
	 * Game & Base
	 */
	public void pushGB(PushCode code, ISFSObject pushObj, List<User> users) {
		push("push.gb", code, pushObj, users);
	}

	public void pushGB(PushCode code, ISFSObject pushObj, User user) {
		push("push.gb", code, pushObj, user);
	}

	/*
	 * Notice
	 */

	public void pushNotice(PushCode code, ISFSObject params, List<User> users) {
		push("push.notice", code, params, users);
	}

	public void pushNotice(PushCode code, ISFSObject params, User user) {
		push("push.notice", code, params, user);
	}

	public void sendMsg(String requestCMD, ISFSObject backObj, User user, WW2ExceptionCode code, boolean succ) {
		if (backObj == null) {
			backObj = new SFSObject();
		}
		backObj.putBool(R.CMD_SUCC, succ);
		if (code != null) {
			backObj.putInt(R.CMD_CODE, code.getValue());
		}
		ext.send(requestCMD, backObj, user);
	}

	public void sendMsg(String cmd, ISFSObject backObj, List<User> users, WW2ExceptionCode code, boolean succ) {
		if (backObj == null) {
			backObj = new SFSObject();
		}
		backObj.putBool(R.CMD_SUCC, succ);
		if (code != null) {
			backObj.putInt(R.CMD_CODE, code.getValue());
		}
		ext.send(cmd, backObj, users);
	}
}
